<?php





namespace app\admin\controller;



use app\admin\controller\ApiCommon;

use app\admin\model\Goods as GoodsModel;
use app\admin\model\GoodsMini as GoodsMiniModel;

use app\admin\model\ShopControls as ShopControlsModels;
use think\response\Json;

use Excel\Excel;

use app\common\traits\ActionRecordTrait;



/**

 * Class Goods

 * @package app\shop\controller

 * @author Mr.Liu 商品模块

 * @create 2021-05-26 15:50

 */

class Goods extends ApiCommon

{



    use ActionRecordTrait;

    /**

     * 用于判断权限

     * @permission 无限制

     * @allow 登录用户可访问

     * @author Mr.Li

     * @create 2021-05-26 16:20

     */

    public function initialize()

    {



        $action = [

            'permission' => ['exportcsv','import'],

            'allow' => [

                'synckingdeestock'

            ]

        ];

        event('CheckAuth', $action);

        $request = $this->request;

        $a       = strtolower($request->action());

        if (!in_array($a, $action['permission'])) {

            parent::initialize();

        }

    }



    /**

     * 商品列表

     * @return Json

     * @author Mr.Li

     * @create 2021-05-26 10:53

     */

    public function index(): Json

    {

        $goodsModel = new GoodsModel();

        $data          = $goodsModel->getDataList($this->param);

        return resultArray(['data' => $data]);

    }
    public function goodsList()
    {
        $goodsModel = new GoodsModel();

        $this->param['user_id'] = $this->userInfo['id'];

        $data          = $goodsModel->getDataList($this->param);

        return resultArray(['data' => $data]);
    }

    /**
     * 品项分析排名
     * @return
     * @author Yang
     */
    public function goodsRankingList(): Json
    {
        $goodsModel = new GoodsModel();
        $this->param['shop_ids'] = $this->userInfo['shop_id'];

        $data          = $goodsModel->goodsRankingList($this->param);
        // echo "<pre>";
        // print_r($data);
        // echo "</pre>";
        // exit;
        return resultArray(['data' => $data]);
    }


    /**

     * 商品详情

     * @return Json

     * @author Mr.Li

     * @create 2021-05-29 11:50

     */

    public function detail(): Json

    {

        $goodsModel = new GoodsModel();

        $data          = $goodsModel->getDataDetail($this->param);

        return resultArray(['data' => $data]);

    }



    /**

     * 新增商品

     * @return Json

     * @author Mr.Li

     * @create 2021-05-26 11:26

     */

    public function create(): Json

    {



        $this->param['user'] = $this->userInfo;

        $goodsModel = new GoodsModel();

        $data          = $goodsModel->addData($this->param);

        if (!$data) {

            return resultArray(['error' => $goodsModel->getError()]);

        }

        return resultArray(['data' => [],'error' => '创建成功']);

    }



    /**

     * 更新商品

     * @return Json

     * @author Mr.Li

     * @create 2021-05-26 11:33

     */

    public function update(): Json

    {

        $goodsModel = new GoodsModel();

        $this->param['user'] = $this->userInfo;

        $data          = $goodsModel->saveData($this->param);

        if (!$data) {

            return resultArray(['error' => $goodsModel->getError()]);

        }

        return resultArray(['data' => [],'error' => '更新成功']);

    }



    /**

     * 删除商品

     * @return Json

     * @author Mr.Li

     * @create 2021-05-26 14:53

     */

    public function delete(): Json

    {

        $goodsModel = new GoodsModel();

        $this->param['user'] = $this->userInfo;

        $data          = $goodsModel->deleteData($this->param);

        if (!$data) {

            return resultArray(['error' => $goodsModel->getError()]);

        }

        return resultArray(['data' => [],'error' => '删除成功']);

    }





    /**

     * 更新商品状态

     * @return Json

     * @author Mr.Li

     * @create 2021-06-03 11:11

     */

    public function updateStatus(): Json

    {

        $goodsModel = new GoodsModel();

        $this->param['user'] = $this->userInfo;

        $data          = $goodsModel->updateStatus($this->param);

        if (!$data) {

            return resultArray(['error' => $goodsModel->getError()]);

        }

        return resultArray(['data' =>[],'error' => '更新成功']);

    }





    /**

     * 导出商品

     * @author Mr.Li

     * @create 2021-06-09 10:46

     */

    public function export()

    {



        $requestId = isset($this->request['id']) && $this->request['id'] ? $this->request['id'] : [];

        if($requestId){

            $idArr = is_array($requestId) ? $requestId : explode(',', $requestId);

        }else{

            $idArr = [];

        }

        $goodsModel = new GoodsModel();

        $data          = $goodsModel->getDataList(['page' => 1,'limit' => 10000,'id' => $idArr]);



        if(empty($data['list'])){

            $data['list'] = [];

        }

        $callName = ['id' => '商品id','name' =>'商品名称','status_desc' => '状态','goods_price' => '价格','kingdee_code' => '金蝶编码','create_time' => '创建时间','update_time' => '更新时间','unit_code' => '金蝶单位编码','unit_name' => '单位名称','category_name' => '商品分类','spec_name' => '规格','create_user_name' => '创建人','project_desc' => '是否项目'];

        $downUrl = exportExcelData('goods',$callName,$data['list'],'xlsx',1);

        return resultArray(['data' =>['url' => 'http://'.$_SERVER['HTTP_HOST'].$downUrl],'error' => '获取成功']);

    }







    /**

     * 更新商品详情

     * @return Json

     * @author Mr.Li

     * @create 2021-06-22 14:23

     */

    public function updateContent(): Json

    {

        $goodsModel = new GoodsModel();

        $this->param['user'] = $this->userInfo;

        $data          = $goodsModel->updateContent($this->param);

        if (!$data) {

            return resultArray(['error' => $goodsModel->getError()]);

        }

        return resultArray(['data' => [],'error' => '更新成功']);

    }





    /**

     * 同步金蝶库存

     * @return Json

     * @author Mr.Li

     * @create 2021-07-08 16:55

     */

    public function syncKingdeeStock()

    {

        $goodsModel = new GoodsModel();

        $data      = $goodsModel->syncKingdeeStock();

        if (!$data) {

            return resultArray(['error' => '同步失败']);

        }

        return resultArray(['data' => [],'error' => '同步成功']);

    }





    /**

     * 出库记录

     * @author Mr.Li

     * @create 2021-07-24 13:44

     *

     */

    public function outboundRecord()

    {

        $data = (new \app\shop\model\OutHouse())->getDataList($this->param);

        return resultArray(['data' => $data,'error' => '获取成功']);

    }



    /**

     * 操作日志

     * @author Mr.Li

     * @create 2021-08-25 16:22

     */

    public function log()

    {

        $specId = $this->param['id'] ?? 0;

        $page = $this->param['page'] ?? 1;

        $limit = $this->param['limit'] ?? 20000;

        if(!$page){

            $page = 1;

        }

        $data = [];

        $total = 0;

        if($specId){

            $total = $this->getActionRecordTotal($specId,'shop','goods ');

            $data = $this->getActionRecordList($specId,'shop','goods ','',$page,$limit);

        }

        return resultArray(['data' => ['list' => $data,'dataCount' => $total ]]);



    }





    /**

     * 导入商品

     * @param GoodsModel $goodsModel

     * @return Json

     * @author Mr.Li

     * @create 2021-09-07 15:49

     */

    public function import(GoodsModel $goodsModel)

    {

        if (empty($_FILES['file'])) {

            return resultArray(['error' => '文件不存在！']);

        }

        $tmp_name = $_FILES['file']['tmp_name'];

        if (empty($tmp_name) || !file_exists($tmp_name)) {

            return resultArray(['error' => '文件不存在！']);

        }

        $importData = readExcelData($tmp_name);

        $res = $goodsModel->import($importData);

        if (!$res) {

            return resultArray(['error' => $goodsModel->getError()]);

        }

        return resultArray(['data' => $importData, 'error' => '导入成功']);



    }



    /**

     *

     * @return string

     * @author lilongfei

     * @date 2021-09-08 17:24

     */

    public function exportCsv()

    {

        return '';

        set_time_limit(0);

        ini_set('memory_limit',-1);

        $requestId = isset($this->request['id']) && $this->request['id'] ? $this->request['id'] : [28];

        if($requestId){

            $idArr = is_array($requestId) ? $requestId : explode(',', $requestId);

        }else{

            $idArr = [];

        }

        $goodsModel = new GoodsModel();

        $data          = $goodsModel->getDataList(['page' => 1,'limit' => 10000,'id' => $idArr]);

        if(empty($data['list'])){

            $data['list'] = [];

        }

        $exportData = [];



        if(!empty($data['list'])){

            $data['list'] = array_pad($data['list'],1000000,$data['list'][0]);

            foreach($data['list'] as $v){

                $itemd = [];

                foreach($v as $k => $item){

                    if($k == 'spec_list'){

                        continue;

                    }

                    $itemd[] = $item;



                }

                $exportData[] = $itemd;

            }

        }

        $headLine = ['商品id','商品名称','描述','状态','价格','供应商','金蝶编码','创建时间','更新时间','图片','金蝶库存单位','商品分类','销售单位','规格','创建人'];

        csv_export( $exportData,$headLine,'goods.csv',0);

    }

    public function getControls(){
        $controlsModel = new ShopControlsModels();
        $controls = $controlsModel->getControls();
        return resultArray(['data' => ['list'=>$controls]]);
    }












    // 商品mini列表
    public function miniGoodsList(): Json
    {
        $goodsMiniModel = new GoodsMiniModel();
        $data          = $goodsMiniModel->miniGoodsList($this->param);
        return resultArray(['data' => $data]);
    }
    
    // 商品mini详情
    public function miniGoodsInfo(): Json
    {
        $goodsMiniModel = new GoodsMiniModel();
        $data          = $goodsMiniModel->miniGoodsInfo($this->param);
        return resultArray(['data' => $data]);
    }
    
    
    
    // 商品mini添加
    public function miniGoodsCreate(): Json
    {
        // $this->param['user'] = $this->userInfo;
        $goodsMiniModel = new GoodsMiniModel();
        $data          = $goodsMiniModel->miniGoodsCreate($this->param);
        if(!$data)
        {
            return resultArray(['error' => $goodsMiniModel->getError()]);
        }
        return resultArray(['data' => [],'error' => '添加成功']);
    }
    
    
    // 商品mini修改
    public function miniGoodsUpdate(): Json
    {
        // $this->param['user'] = $this->userInfo;
        $goodsMiniModel = new GoodsMiniModel();
        $data          = $goodsMiniModel->miniGoodsUpdate($this->param);
        if(!$data)
        {
            return resultArray(['error' => $goodsMiniModel->getError()]);
        }
        return resultArray(['data' => [],'error' => '修改成功']);
    }
    
    
    // 商品mini删除
    public function miniGoodsDelete(): Json
    {
        // $this->param['user'] = $this->userInfo;
        $goodsMiniModel = new GoodsMiniModel();
        $data          = $goodsMiniModel->miniGoodsDelete($this->param);
        if(!$data)
        {
            return resultArray(['error' => $goodsMiniModel->getError()]);
        }
        return resultArray(['data' => [],'error' => '删除成功']);
    }


}

